home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 16XMX79 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  7.9 KB  |  307 lines

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.accessibility.AccessibleContext;
  4. import com.sun.java.swing.text.AttributeSet;
  5. import com.sun.java.swing.text.BadLocationException;
  6. import com.sun.java.swing.text.Document;
  7. import com.sun.java.swing.text.Element;
  8. import com.sun.java.swing.text.JTextComponent;
  9. import com.sun.java.swing.text.PlainDocument;
  10. import java.awt.Component;
  11. import java.awt.Container;
  12. import java.awt.Dimension;
  13. import java.awt.Font;
  14. import java.awt.FontMetrics;
  15. import java.awt.Rectangle;
  16.  
  17. public class JTextArea extends JTextComponent {
  18.    private int rows;
  19.    private int columns;
  20.    private int columnWidth;
  21.    private int rowHeight;
  22.    private boolean wrap;
  23.    private boolean word;
  24.  
  25.    public JTextArea() {
  26.       this((Document)null, (String)null, 0, 0);
  27.    }
  28.  
  29.    public JTextArea(int rows, int columns) {
  30.       this((Document)null, (String)null, rows, columns);
  31.    }
  32.  
  33.    public JTextArea(Document doc) {
  34.       this(doc, (String)null, 0, 0);
  35.    }
  36.  
  37.    public JTextArea(Document doc, String text, int rows, int columns) {
  38.       this.rows = rows;
  39.       this.columns = columns;
  40.       if (doc == null) {
  41.          doc = this.createDefaultModel();
  42.       }
  43.  
  44.       ((JTextComponent)this).setDocument(doc);
  45.       if (text != null) {
  46.          ((JTextComponent)this).setText(text);
  47.       }
  48.  
  49.    }
  50.  
  51.    public JTextArea(String text) {
  52.       this((Document)null, text, 0, 0);
  53.    }
  54.  
  55.    public JTextArea(String text, int rows, int columns) {
  56.       this((Document)null, text, rows, columns);
  57.    }
  58.  
  59.    public void append(String str) {
  60.       Document doc = ((JTextComponent)this).getDocument();
  61.       if (doc != null) {
  62.          try {
  63.             doc.insertString(doc.getLength(), str, (AttributeSet)null);
  64.          } catch (BadLocationException var3) {
  65.          }
  66.       }
  67.  
  68.    }
  69.  
  70.    protected Document createDefaultModel() {
  71.       return new PlainDocument();
  72.    }
  73.  
  74.    public AccessibleContext getAccessibleContext() {
  75.       if (super.accessibleContext == null) {
  76.          super.accessibleContext = new AccessibleJTextArea(this);
  77.       }
  78.  
  79.       return super.accessibleContext;
  80.    }
  81.  
  82.    public int getColumns() {
  83.       return this.columns;
  84.    }
  85.  
  86.    protected int getColumnWidth() {
  87.       if (this.columnWidth == 0) {
  88.          FontMetrics metrics = ((Component)this).getFontMetrics(((Component)this).getFont());
  89.          this.columnWidth = metrics.charWidth('m');
  90.       }
  91.  
  92.       return this.columnWidth;
  93.    }
  94.  
  95.    public int getLineCount() {
  96.       Element map = ((JTextComponent)this).getDocument().getDefaultRootElement();
  97.       return map.getElementCount();
  98.    }
  99.  
  100.    public int getLineEndOffset(int line) throws BadLocationException {
  101.       Element map = ((JTextComponent)this).getDocument().getDefaultRootElement();
  102.       if (line < 0) {
  103.          throw new BadLocationException("Negative line", -1);
  104.       } else if (line >= map.getElementCount()) {
  105.          throw new BadLocationException("No such line", ((JTextComponent)this).getDocument().getLength() + 1);
  106.       } else {
  107.          Element lineElem = map.getElement(line);
  108.          return lineElem.getEndOffset();
  109.       }
  110.    }
  111.  
  112.    public int getLineOfOffset(int offset) throws BadLocationException {
  113.       Document doc = ((JTextComponent)this).getDocument();
  114.       if (offset < 0) {
  115.          throw new BadLocationException("Can't translate offset to line", -1);
  116.       } else if (offset > doc.getLength()) {
  117.          throw new BadLocationException("Can't translate offset to line", doc.getLength() + 1);
  118.       } else {
  119.          Element map = ((JTextComponent)this).getDocument().getDefaultRootElement();
  120.          return map.getElementIndex(offset);
  121.       }
  122.    }
  123.  
  124.    public int getLineStartOffset(int line) throws BadLocationException {
  125.       Element map = ((JTextComponent)this).getDocument().getDefaultRootElement();
  126.       if (line < 0) {
  127.          throw new BadLocationException("Negative line", -1);
  128.       } else if (line >= map.getElementCount()) {
  129.          throw new BadLocationException("No such line", ((JTextComponent)this).getDocument().getLength() + 1);
  130.       } else {
  131.          Element lineElem = map.getElement(line);
  132.          return lineElem.getStartOffset();
  133.       }
  134.    }
  135.  
  136.    public boolean getLineWrap() {
  137.       return this.wrap;
  138.    }
  139.  
  140.    public Dimension getMinimumSize() {
  141.       return this.columns == 0 && this.rows == 0 ? super.getMinimumSize() : this.getPreferredSize();
  142.    }
  143.  
  144.    public Dimension getPreferredScrollableViewportSize() {
  145.       Dimension size = super.getPreferredScrollableViewportSize();
  146.       size = size == null ? new Dimension(400, 400) : size;
  147.       size.width = this.columns == 0 ? size.width : this.columns * this.getColumnWidth();
  148.       size.height = this.rows == 0 ? size.height : this.rows * this.getRowHeight();
  149.       return size;
  150.    }
  151.  
  152.    public Dimension getPreferredSize() {
  153.       Dimension d = super.getPreferredSize();
  154.       d = d == null ? new Dimension(400, 400) : d;
  155.       if (this.columns != 0) {
  156.          d.width = Math.max(d.width, this.columns * this.getColumnWidth());
  157.       }
  158.  
  159.       if (this.rows != 0) {
  160.          d.height = Math.max(d.height, this.rows * this.getRowHeight());
  161.       }
  162.  
  163.       return d;
  164.    }
  165.  
  166.    protected int getRowHeight() {
  167.       if (this.rowHeight == 0) {
  168.          FontMetrics metrics = ((Component)this).getFontMetrics(((Component)this).getFont());
  169.          this.rowHeight = metrics.getHeight();
  170.       }
  171.  
  172.       return this.rowHeight;
  173.    }
  174.  
  175.    public int getRows() {
  176.       return this.rows;
  177.    }
  178.  
  179.    public boolean getScrollableTracksViewportWidth() {
  180.       return this.wrap;
  181.    }
  182.  
  183.    public int getScrollableUnitIncrement(Rectangle visibleRect, int orientation, int direction) {
  184.       switch (orientation) {
  185.          case 0:
  186.             return this.getColumnWidth();
  187.          case 1:
  188.             return this.getRowHeight();
  189.          default:
  190.             throw new IllegalArgumentException("Invalid orientation: " + orientation);
  191.       }
  192.    }
  193.  
  194.    public int getTabSize() {
  195.       int size = 8;
  196.       Document doc = ((JTextComponent)this).getDocument();
  197.       if (doc != null) {
  198.          Integer i = (Integer)doc.getProperty("tabSize");
  199.          if (i != null) {
  200.             size = i;
  201.          }
  202.       }
  203.  
  204.       return size;
  205.    }
  206.  
  207.    public String getUIClassID() {
  208.       return "TextAreaUI";
  209.    }
  210.  
  211.    public boolean getWrapStyleWord() {
  212.       return this.word;
  213.    }
  214.  
  215.    public void insert(String str, int pos) {
  216.       Document doc = ((JTextComponent)this).getDocument();
  217.       if (doc != null) {
  218.          try {
  219.             doc.insertString(pos, str, (AttributeSet)null);
  220.          } catch (BadLocationException var5) {
  221.             throw new IllegalArgumentException(((Throwable)var5).getMessage());
  222.          }
  223.       }
  224.  
  225.    }
  226.  
  227.    public boolean isManagingFocus() {
  228.       return true;
  229.    }
  230.  
  231.    protected String paramString() {
  232.       return super.paramString() + ",rows=" + this.rows + ",columns=" + this.columns;
  233.    }
  234.  
  235.    public void replaceRange(String str, int start, int end) {
  236.       if (end < start) {
  237.          throw new IllegalArgumentException("end before start");
  238.       } else {
  239.          Document doc = ((JTextComponent)this).getDocument();
  240.          if (doc != null) {
  241.             try {
  242.                doc.remove(start, end - start);
  243.                doc.insertString(start, str, (AttributeSet)null);
  244.             } catch (BadLocationException var6) {
  245.                throw new IllegalArgumentException(((Throwable)var6).getMessage());
  246.             }
  247.          }
  248.  
  249.       }
  250.    }
  251.  
  252.    public void setColumns(int columns) {
  253.       int oldVal = this.columns;
  254.       if (columns < 0) {
  255.          throw new IllegalArgumentException("columns less than zero.");
  256.       } else {
  257.          if (columns != oldVal) {
  258.             this.columns = columns;
  259.             ((Container)this).invalidate();
  260.          }
  261.  
  262.       }
  263.    }
  264.  
  265.    public void setFont(Font f) {
  266.       super.setFont(f);
  267.       this.rowHeight = 0;
  268.       this.columnWidth = 0;
  269.       ((JComponent)this).revalidate();
  270.    }
  271.  
  272.    public void setLineWrap(boolean wrap) {
  273.       boolean old = this.wrap;
  274.       this.wrap = wrap;
  275.       ((JComponent)this).firePropertyChange("LineWrap", old, wrap);
  276.    }
  277.  
  278.    public void setRows(int rows) {
  279.       int oldVal = this.rows;
  280.       if (rows < 0) {
  281.          throw new IllegalArgumentException("rows less than zero.");
  282.       } else {
  283.          if (rows != oldVal) {
  284.             this.rows = rows;
  285.             ((Container)this).invalidate();
  286.          }
  287.  
  288.       }
  289.    }
  290.  
  291.    public void setTabSize(int size) {
  292.       Document doc = ((JTextComponent)this).getDocument();
  293.       if (doc != null) {
  294.          int old = this.getTabSize();
  295.          doc.putProperty("tabSize", new Integer(size));
  296.          ((JComponent)this).firePropertyChange("TabSize", old, size);
  297.       }
  298.  
  299.    }
  300.  
  301.    public void setWrapStyleWord(boolean word) {
  302.       boolean old = this.word;
  303.       this.word = word;
  304.       ((JComponent)this).firePropertyChange("WrapStyleWord", old, word);
  305.    }
  306. }
  307.